home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / basic / qbnws104.zip / IQUEUE.ZIP / LSTACK.ASM < prev    next >
Assembly Source File  |  1990-05-14  |  4KB  |  101 lines

  1.         page ,132
  2.         title   LSTACK - Add a long-integer "stack" to BC/QB
  3.         subttl  Copyright 1990, Editing Services Co.
  4.         comment |
  5.  
  6. Implements a long-integer LIFO stack by adding five procedures to QB/BC:
  7. two SUBs and three FUNCTIONs.  See LQUEUE for a FIFO stack.
  8.  
  9.         DECLARE SUB      LSpush (ival%)  'put an item on the stack
  10.         DECLARE FUNCTION LSpop% ()       'get an item off the stack
  11.         DECLARE FUNCTION LSfree% ()      'TRUE if any room on stack
  12.         DECLARE FUNCTION LSavail% ()     'TRUE if anything on stack
  13.         DECLARE SUB      LSclear ()      'clear the stack
  14.         |
  15.                                                                         page +
  16.         .model medium, basic
  17.         .data
  18.  
  19. LSP     dw      0               ; DGROUP storage for stack pointer
  20.  
  21.         .code
  22.  
  23. MaxLSP  equ     255             ; determines max items in stack
  24.  
  25. LStack  dw      (2 * (MaxLSP + 1)) dup (-1)
  26.                                                                         page +
  27. Iperr:: INT     4               ; report an "Overflow" to QB/BC
  28.  
  29. LSpush          PROC lval
  30.  
  31. ; Accepts a long-integer value and places it onto our stack.
  32. ; Generates an Overflow Error in QB if there is no room on the stack.
  33.  
  34.         mov     bx, lval        ; get pointer to passed value
  35.         mov     ax, [bx]        ; hold actual value
  36.         mov     dx, 2[bx]       ; high part too
  37.         mov     bx, LSP         ; get the current stack pointer
  38.         inc     bx              ; increment it first
  39.         cmp     bx, MaxLSP      ; test against the allowed maximum depth
  40.         ja      Iperr           ; if stack already full, Overflow Error
  41.         mov     LSP, bx         ; otherwise, save new SP
  42.         shl     bx, 1           ; turn SP into a word pointer
  43.         shl     bx, 1           ; then into a dword pointer
  44.         mov     LStack[bx], ax  ; stack the low part of the item
  45.         add     bx, 2
  46.         mov     LStack[bx], dx  ; then the high part
  47.         ret
  48.  
  49. LSpush          ENDP
  50.  
  51. LSpop           PROC
  52.  
  53. ; Returns the most recently pushed value from our stack.
  54. ; Generates an Overflow Error if there are no values on the stack.
  55.  
  56.         mov     bx, LSP         ; get current stack pointer
  57.         or      bx, bx          ; is it zero?
  58.         jz      Iperr           ; yes, stack is empty: Overflow error
  59.         shl     bx, 1           ; turn LSP into a word pointer
  60.         shl     bx, 1           ; and then a dword pointer
  61.         mov     ax, LStack[bx]  ; retrieve low half of value from stack
  62.         add     bx, 2
  63.         mov     dx, LStack[bx]  ; retrieve high part of value, too
  64.         dec     LSP             ; decrement saved LSP
  65.         ret                     ; and return value in DX:AX
  66.  
  67. LSpop           ENDP
  68.  
  69. LSclear         PROC
  70.  
  71. ; "Clear" the stack by resetting our stack pointer to zero.
  72.  
  73.         mov     LSP, 0
  74.         ret
  75.  
  76. LSclear         ENDP
  77.  
  78. LSfree          PROC
  79.  
  80. ; Returns TRUE if there is any space left in the stack.
  81.  
  82.         xor     ax, ax          ; assume there's no room
  83.         cmp     LSP, MaxLSP     ; set carry if room left
  84.         sbb     ax, ax          ; adjust result to -1 if room
  85.         ret
  86.  
  87. LSfree          ENDP
  88.  
  89. LSavail         PROC
  90.  
  91. ; Returns TRUE if there are any data items on the stack
  92.  
  93.         xor     ax, ax          ; assume no items present
  94.         cmp     ax, LSP         ; set carry if LSP non-zero
  95.         sbb     ax, ax          ; adjust result to TRUE if stack used
  96.         ret
  97.  
  98. LSavail         ENDP
  99.                                                                         page +
  100.         end
  101.